//
//  MineAllOrderViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/5.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "MineAllOrderViewController.h"
#import "MineOrderAllCell.h"
#import "MineOrderPaidCell.h"
#import "MineOrderUsedCell.h"
#import "MineOrderEvaluateCell.h"
#import "MineOrderRefundCell.h"

#import "RefundViewController.h"//退款
#import "CommitViewController.h"
#import "PayViewController.h"

#import "OrderDetailViewController.h"
#import "MineViewController.h"
#import "AllOrderModel.h"
#import "SubmitOrderViewController.h"
@interface MineAllOrderViewController ()<UITableViewDelegate,UITableViewDataSource, UIGestureRecognizerDelegate, UIAlertViewDelegate>

@property (nonatomic, strong) UIButton *button;

@property (nonatomic, strong) UIButton *allBtn;
@property (nonatomic, strong) UIButton *paidBtn;
@property (nonatomic, strong) UIButton *usedBtn;
@property (nonatomic, strong) UIButton *evaluateBtn;//评价
@property (nonatomic, strong) UIButton *refundBtn;//退款

@property (nonatomic, strong) NSMutableArray *allArray;//全部
@property (nonatomic, strong) NSMutableArray *useArray;//待使用
@property (nonatomic, strong) NSMutableArray *payArray;//待付款
@property (nonatomic, strong) NSMutableArray *evaluteArray;//待评价
@property (nonatomic, strong) NSMutableArray *refundArray;//待退款

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *alert;
@property (nonatomic, strong) UIView *aview;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) LoadingView *load;
@property (nonatomic, strong) UIImageView *iv;
@end

@implementation MineAllOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self addBackItem];
    
    _useArray = [NSMutableArray arrayWithCapacity:0];
    _refundArray = [NSMutableArray arrayWithCapacity:0];
    _allArray = [NSMutableArray arrayWithCapacity:0];
    _payArray = [NSMutableArray arrayWithCapacity:0];
    _evaluteArray = [NSMutableArray arrayWithCapacity:0];

    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(backItemAction)];
    
    self.title = @"全部订单";
    [self initGWView];
    [self createAlertView];
    
    if([ GW_Request sharedInstance].noNetwork)
    {
        _load.hidden = NO;
    }
    else
    {
        _load.hidden = YES;
    }
}


- (void)handleRefresh:(UIButton *)sender
{
    NSString *str = _status;
    [self DoRefresh:str];
}

- (void)backItemAction
{
    if([self.flag isEqualToString:@"订单"])
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    else
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)DoRefresh:(NSString *)status
{
    _page = 1;
    status = _status;
    [self startNetwork:status];
}

- (void)Append:(NSString *)status
{
    status = _status;
    [self startNetwork:status];
}

- (void)startNetwork:(NSString *)statusStr
{
    
    NSDictionary *param = @{@"status":statusStr,
                            @"type"  :@"list",
                            @"page"  :@(_page)
                            };
    @weakobj(self);
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/order/getOrder?status=%@&type=%@&page=%ld", GW_Domain, statusStr, @"list", _page] parameters:param completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"%@", resultDict);
        @strongobj(self);
        if(self.page == 1)
        {
            [_allArray removeAllObjects];
            [_payArray removeAllObjects];
            [_useArray removeAllObjects];
            [_evaluteArray removeAllObjects];
            [_refundArray removeAllObjects];
        }
      
         if([statusStr isEqualToString:@"0"])//待付款
        {
            NSArray *arr = resultDict[@"data"];
            for (NSDictionary *dict in resultDict[@"data"]) {
                AllOrderModel *model = [GW_Tool getObjFromDictionary:dict byClassName:@"AllOrderModel"];
                [_payArray addObject:model];
                MESSAGE_TOAST(resultDict[@"message"]);
            }
            if(_payArray.count > 0)
                _load.hidden = YES;
            else
                _load.hidden = NO;
            
            if (arr.count < kPageSize) {
                [UIView animateWithDuration:0.3 animations:^{
                    self.tableView.mj_footer.hidden = YES;// 没有数据说明是最后一页了，隐藏上拉加载
                }];
            }
            else {
                self.tableView.mj_footer.hidden = NO;
            }
        }
        if([statusStr isEqualToString:@"99"])//全部
        {
            NSArray *arr = resultDict[@"data"];
            for (NSDictionary *dict in resultDict[@"data"]) {
                AllOrderModel *model = [GW_Tool getObjFromDictionary:dict byClassName:@"AllOrderModel"];
                [_allArray addObject:model];
                MESSAGE_TOAST(resultDict[@"message"]);
            }
            
            if(_allArray.count > 0)
                _load.hidden = YES;
            else
                _load.hidden = NO;
            
            if (arr.count < kPageSize) {
                [UIView animateWithDuration:0.3 animations:^{
                    self.tableView.mj_footer.hidden = YES;// 没有数据说明是最后一页了，隐藏上拉加载
                }];
            }
            else {
                self.tableView.mj_footer.hidden = NO;
            }
        }
        else if([statusStr isEqualToString:@"1"])//待使用
        {
            NSArray *arr = resultDict[@"data"];
            for (NSDictionary *dict in resultDict[@"data"]) {
                AllOrderModel *model = [GW_Tool getObjFromDictionary:dict byClassName:@"AllOrderModel"];
                [_useArray addObject:model];
            }
            MESSAGE_TOAST(resultDict[@"message"]);

            if(_useArray.count > 0)
                _load.hidden = YES;
            else
                _load.hidden = NO;
            
            if (arr.count < kPageSize) {
                [UIView animateWithDuration:0.3 animations:^{
                    self.tableView.mj_footer.hidden = YES;// 没有数据说明是最后一页了，隐藏上拉加载
                }];
            }
            else {
                self.tableView.mj_footer.hidden = NO;
            }
        }
        else if([statusStr isEqualToString:@"5"])//待评价
        {
            NSArray *arr = resultDict[@"data"];
            for (NSDictionary *dict in resultDict[@"data"]) {
                AllOrderModel *model = [GW_Tool getObjFromDictionary:dict byClassName:@"AllOrderModel"];
                [_evaluteArray addObject:model];
            }
            MESSAGE_TOAST(resultDict[@"message"]);

            if(_evaluteArray.count > 0)
                _load.hidden = YES;
            else
                _load.hidden = NO;
            
            if (arr.count < kPageSize) {
                [UIView animateWithDuration:0.3 animations:^{
                    self.tableView.mj_footer.hidden = YES;// 没有数据说明是最后一页了，隐藏上拉加载
                }];
            }
            else {
                self.tableView.mj_footer.hidden = NO;
            }
        }
        else //  6 退款
        {
            NSArray *arr = resultDict[@"data"];
            for (NSDictionary *dict in resultDict[@"data"]) {
                AllOrderModel *model = [GW_Tool getObjFromDictionary:dict byClassName:@"AllOrderModel"];
                [_refundArray addObject:model];
            }
            MESSAGE_TOAST(resultDict[@"message"]);
            if(_refundArray.count > 0)
                _load.hidden = YES;
            else
                _load.hidden = NO;
            
            if (arr.count < kPageSize) {
                [UIView animateWithDuration:0.3 animations:^{
                    self.tableView.mj_footer.hidden = YES;// 没有数据说明是最后一页了，隐藏上拉加载
                }];
            }
            else {
                self.tableView.mj_footer.hidden = NO;
            }
            
        }
        self.page++;
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [_tableView reloadData];
        
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", errorMsg);
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}

- (void)initGWView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, GWIDTH, GHEIGHT - 44 - 64 ) style:UITableViewStylePlain];
    _tableView.separatorColor = ColorClear;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[MineOrderPaidCell class] forCellReuseIdentifier:@"paid"];
    [_tableView registerClass:[MineOrderUsedCell class] forCellReuseIdentifier:@"use"];
    [_tableView registerClass:[MineOrderEvaluateCell class] forCellReuseIdentifier:@"evaluate"];
    [_tableView registerClass:[MineOrderRefundCell class] forCellReuseIdentifier:@"refund"];
    [_tableView registerClass:[MineOrderAllCell class] forCellReuseIdentifier:@"all"];
    [self.view addSubview:_tableView];
    
    _page = 1;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(DoRefresh:)];
    _tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(Append:)];//加载
    [_tableView.mj_header beginRefreshing];
    
    NSArray *array = @[@"全部", @"待付款", @"待使用", @"待评价", @"退款"];
    for(int i = 1 ; i < 6; i++)
    {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake((GWIDTH/5) * (i-1), 0, GWIDTH / 5, 44);
        [_button setTitle:array[i-1] forState:0];
        [_button setTitleColor:ColorFrom16(0x757982) forState:0];
        _button.titleLabel.font = PFRFONT(14);
        _button.backgroundColor = ColorWhite;
        _button.tag = 1000 + i;
        [self.view addSubview:_button];
        if(_button.tag == 1001)
        {
            _allBtn = _button;
        }else if (_button.tag == 1002)
        {
            _paidBtn = _button;
        }
        else if (_button.tag == 1003)
        {
            _usedBtn = _button;
        }
        else if(_button.tag == 1004)
        {
            _evaluateBtn = _button;
        }
        else
        {
            _refundBtn = _button;
        }
   }
    
    [_allBtn addTarget:self action:@selector(handleAllBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_paidBtn addTarget:self action:@selector(handlePaidBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_usedBtn addTarget:self action:@selector(handleUsedBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_evaluateBtn addTarget:self action:@selector(handleEvaluateBtn:) forControlEvents:UIControlEventTouchUpInside];
     [_refundBtn addTarget:self action:@selector(handleRefundBtn:) forControlEvents:UIControlEventTouchUpInside];
    if([self.titleName isEqualToString:@"全部订单"])
    {
        [_allBtn setTitleColor:ColorFrom16(0xF5CB37) forState:0];
        _allBtn.titleLabel.font = PFMFONT(14);
        _allBtn.selected = YES;
        _status = @"99";
        [self DoRefresh:_status];
    }
    
    else if ([self.titleName isEqualToString:@"待付款"])
    {
        
        _paidBtn.selected = YES;
        [_paidBtn setTitleColor:ColorFrom16(0xF5CB37) forState:0];
        _paidBtn.titleLabel.font = PFMFONT(14);
//        [_tableView reloadData];
        _status = @"0";
        [self DoRefresh:_status];
    }
    else if ([self.titleName isEqualToString:@"待使用"])
    {
        _status = @"1";
        [self DoRefresh:_status];
        _usedBtn.selected = YES;
        [_usedBtn setTitleColor:ColorFrom16(0xF5CB37) forState:0];
        _usedBtn.titleLabel.font = PFMFONT(14);
    }
    else if ([self.titleName isEqualToString:@"待评价"])
    {
        _status = @"5";
        [self DoRefresh:_status];
        _evaluateBtn.selected = YES;
        [_evaluateBtn setTitleColor:ColorFrom16(0xF5CB37) forState:0];
        _evaluateBtn.titleLabel.font = PFMFONT(14);
        
    }
    else
    {
        _status = @"6";
        [self DoRefresh:_status];
        _refundBtn.selected = YES;
        [_refundBtn setTitleColor:ColorFrom16(0xF5CB37) forState:0];
        _refundBtn.titleLabel.font = PFMFONT(14);
    }
    
 
    _load = [[LoadingView alloc] initWithFrame:CGRectMake(0, 44 , GWIDTH, GHEIGHT - 44 - 64)];
    [_load setReminderLabelTitle:@"您还没有订单哦~" andImage:@"order_empty"];
    [_load.refreshBtn addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventTouchUpInside];
    _load.hidden = YES;
    [self.view addSubview:_load];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(_paidBtn.selected == YES)
    {
        return _payArray.count;
    }
    else if (_usedBtn.selected == YES)
    {
        return _useArray.count;
    }
    else if (_evaluateBtn.selected == YES)
        return _evaluteArray.count;
    else if(_refundBtn.selected == YES)
        return _refundArray.count;
    else
    {
        return self.allArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ( _paidBtn.selected == YES)
        return 134+44;//待付款
    else if ( _usedBtn.selected == YES)
        return 178;//待使用
    else if ( _evaluateBtn.selected == YES)
        return 178;//待评价
    if( _refundBtn.selected == YES)
        return 178;//退款
    else
        return 134;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if( _paidBtn.selected == YES)
    {
        AllOrderModel *model = self.payArray[indexPath.row];
        MineOrderPaidCell *cell = [tableView dequeueReusableCellWithIdentifier:@"paid"];
        [cell.gotoPaidBtn addTarget:self action:@selector(handlePayAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.gotoPaidBtn.tag = 3000 + indexPath.row;
        [cell configViewWithModel:model];
        return cell;
    }
    else if ( _usedBtn.selected == YES)
    {
        AllOrderModel *model = self.useArray[indexPath.row];
        MineOrderUsedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"use"];
        [cell.refundBtn addTarget:self action:@selector(handleGotoBtn:) forControlEvents:UIControlEventTouchUpInside];
        [cell.gotoPaidBtn addTarget:self action:@selector(useBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.gotoPaidBtn.tag = 2000 + indexPath.row;//立即使用的按钮
        cell.refundBtn.tag = 1000 + indexPath.row;//申请退款
        [cell configViewWithModel:model];
        return cell;
    }
    else if ( _evaluateBtn.selected == YES)
    {
        AllOrderModel *model = self.evaluteArray[indexPath.row];
        MineOrderEvaluateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"evaluate"];
        cell.evaluteBtn.tag = 100 + indexPath.row;//评价
        cell.gotoPaidBtn.tag = 200 + indexPath.row;//立即购买
        [cell.evaluteBtn addTarget:self action:@selector(evaluteBtnAvtion:) forControlEvents:UIControlEventTouchUpInside];//立即表白
        [cell.gotoPaidBtn addTarget:self action:@selector(handleGotoPayBtn:) forControlEvents:UIControlEventTouchUpInside];//再次购买
        [cell configViewWithModel:model];
        return cell;
    }
    else if( _refundBtn.selected == YES)
    {
        AllOrderModel *model = self.refundArray[indexPath.row];

        MineOrderRefundCell *cell = [tableView dequeueReusableCellWithIdentifier:@"refund"];
        [cell configViewWithModel:model];
        return cell;
    }
    else
    {
        AllOrderModel *model = self.allArray[indexPath.row];
        MineOrderAllCell *cell = [tableView dequeueReusableCellWithIdentifier:@"all"];
        [cell configViewWithModel:model];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    OrderDetailViewController *orderVC = [OrderDetailViewController new];
    if ( _usedBtn.selected == YES)
    {
        AllOrderModel *model = self.useArray[indexPath.row];
        orderVC.ordersn = model.order_sn;
        //待使用跳转订单详情
        [self.navigationController pushViewController:orderVC animated:YES];
    }
    else if (_allBtn.selected == YES)//全部
    {
        AllOrderModel *model = self.allArray[indexPath.row];
        if([model.status isEqualToString:@"已支付"])//跳详情
        {
            OrderDetailViewController *detail = [OrderDetailViewController new];
            detail.ordersn = model.order_sn;
            [self.navigationController pushViewController:detail animated:YES];
        }
        else if([model.status isEqualToString:@"待支付"])
        {
            SubmitOrderViewController *submitVC = [SubmitOrderViewController new];
            submitVC.goodsName = model.goods_name;
            submitVC.goodId = model.id;
            submitVC.sale_price = model.sale_price;
            submitVC.img = model.goods_img;
            [self.navigationController pushViewController:submitVC animated:YES];
        }
        else if([model.status isEqualToString:@"已使用"])
        {
            CommitViewController *VC = [CommitViewController new];
            VC.orderString = model.order_sn;
            [self.navigationController pushViewController:VC animated:YES];
        }
    }
    else
    {
        return;
    }
    
}


- (void)handleGotoPayBtn:(UIButton *)sender
{
    SubmitOrderViewController *order = [SubmitOrderViewController new];
    AllOrderModel *model = self.evaluteArray[sender.tag - 200];
    order.goodsName = model.goods_name;
    order.goodId = model.id;
    order.sale_price = model.sale_price;
    order.img = model.goods_img;
    [self.navigationController pushViewController:order animated:YES];
}

//立即付款
- (void)handlePayAction:(UIButton *)sender
{
    AllOrderModel *model = self.payArray[sender.tag - 3000];
    PayViewController *pay = [PayViewController new];
    pay.orderId = model.order_sn;
    [self.navigationController pushViewController:pay animated:YES];
}

//立即表白
- (void)evaluteBtnAvtion:(UIButton *)sender
{
    AllOrderModel *model = self.evaluteArray[sender.tag - 100];
    CommitViewController *VC = [CommitViewController new];
    VC.orderString = model.order_sn;
    [self.navigationController pushViewController:VC animated:YES];
}

//立即使用按钮相应方法
- (void)useBtnAction:(UIButton *)sender
{
    AllOrderModel *model = self.useArray[sender.tag - 2000];
    
    [self refreshCode:model.order_sn];
}

// -----无限请求   刷新二维码
- (void)refreshCode:(NSString *)orderSn
{
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/order/getOrderDet?orderSn=%@&type=%@", GW_Domain, orderSn, @"3"] parameters:nil completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"%@", resultDict);
        if([resultDict[@"data"][@"status"] isEqualToString:@"1"])//如果是1  弹出二维码
        {
            NSString *str = [NSString stringWithFormat:@"%@/index/qrcodeOrder?order_id=%@", GW_Domain, orderSn];
            str = [NetWork handleUrlStr:str withDict:nil];
            [_iv sd_setImageWithURL:[NSURL URLWithString:str]];
            _alert.hidden = NO;
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"订单状态不正确,请刷新重试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            
        }
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", errorMsg);
    }];
}


//申请退款
- (void)handleGotoBtn:(UIButton *)sender
{
    AllOrderModel *model = self.useArray[sender.tag - 1000];
    RefundViewController *refundVC = [RefundViewController new];
    refundVC.orderId = model.order_sn;
    refundVC.goodName = model.goods_name;
    refundVC.sale_price = model.sale_price;
    [self.navigationController pushViewController:refundVC animated:YES];
}

- (void)handleAllBtn:(UIButton *)sender
{
    sender.selected = YES;
    _paidBtn.selected = NO;
    _usedBtn.selected = NO;
    _evaluateBtn.selected = NO;
    _refundBtn.selected = NO;
    [sender setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    sender.titleLabel.font = PFMFONT(14);
    
    [_paidBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _paidBtn.titleLabel.font = PFRFONT(14);
    [_usedBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _usedBtn.titleLabel.font = PFRFONT(14);
    [_evaluateBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _evaluateBtn.titleLabel.font = PFRFONT(14);
    [_refundBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _refundBtn.titleLabel.font = PFRFONT(14);
    _status = @"99";
    [self DoRefresh:_status];
}

- (void)handlePaidBtn:(UIButton *)sender
{
    sender.selected = YES;
    _allBtn.selected = NO;
    _usedBtn.selected = NO;
    _evaluateBtn.selected = NO;
    _refundBtn.selected = NO;

    [sender setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    sender.titleLabel.font = PFMFONT(14);
    
    [_allBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _allBtn.titleLabel.font = PFRFONT(14);
    [_usedBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _usedBtn.titleLabel.font = PFRFONT(14);
    [_evaluateBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _evaluateBtn.titleLabel.font = PFRFONT(14);
    [_refundBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _refundBtn.titleLabel.font = PFRFONT(14);
    _status = @"0";
    [self DoRefresh:_status];
}

- (void)handleUsedBtn:(UIButton *)sender
{
    sender.selected = YES;
    _paidBtn.selected = NO;
    _allBtn.selected = NO;
    _evaluateBtn.selected = NO;
    _refundBtn.selected = NO;

    [sender setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    sender.titleLabel.font = PFMFONT(14);
    
    [_allBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _allBtn.titleLabel.font = PFRFONT(14);
    [_paidBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _paidBtn.titleLabel.font = PFRFONT(14);
    [_evaluateBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _evaluateBtn.titleLabel.font = PFRFONT(14);
    [_refundBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _refundBtn.titleLabel.font = PFRFONT(14);
    _status = @"1";//待使用
    [self DoRefresh:_status];

}

- (void)handleEvaluateBtn:(UIButton *)sender
{
    sender.selected = YES;
    _paidBtn.selected = NO;
    _usedBtn.selected = NO;
    _allBtn.selected = NO;
    _refundBtn.selected = NO;

    [sender setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    sender.titleLabel.font = PFMFONT(14);
    
    [_allBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _allBtn.titleLabel.font = PFRFONT(14);
    [_usedBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _usedBtn.titleLabel.font = PFRFONT(14);
    [_paidBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _paidBtn.titleLabel.font = PFRFONT(14);
    [_refundBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _refundBtn.titleLabel.font = PFRFONT(14);
    _status = @"5";//待评价
    [self DoRefresh:_status];

}

- (void)handleRefundBtn:(UIButton *)sender
{
    sender.selected = YES;
    _paidBtn.selected = NO;
    _usedBtn.selected = NO;
    _evaluateBtn.selected = NO;
    _allBtn.selected = NO;

    [sender setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    sender.titleLabel.font = PFMFONT(14);
    
    [_allBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _allBtn.titleLabel.font = PFRFONT(14);
    [_usedBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _usedBtn.titleLabel.font = PFRFONT(14);
    [_evaluateBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _evaluateBtn.titleLabel.font = PFRFONT(14);
    [_paidBtn setTitleColor:ColorFrom16(0x757982) forState:0];
    _paidBtn.titleLabel.font = PFRFONT(14);
    _status = @"6";//退款
    [self DoRefresh:_status];
}


- (void)createAlertView
{
    _alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT)];
    _alert.hidden = YES;
    _alert.backgroundColor = ColorFrom16A(0x000000, 0.8);
    [self.navigationController.view addSubview:_alert];
    [_alert bringSubviewToFront:[UIWindow new]];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)];
    tap.delegate = self;
    [_alert addGestureRecognizer:tap];
    
    _aview = [UIView new];
    _aview.layer.backgroundColor = ColorWhite.CGColor;
    _aview.layer.cornerRadius = 12;
    [_alert addSubview:_aview];
    [_aview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_alert).offset(246);
        make.centerX.equalTo(_alert.mas_centerX);
        make.width.equalTo(@270);
        make.height.equalTo(@181);
    }];

    
    _iv = [UIImageView new];
    _iv.image = IMAGE(@"");
    [_aview addSubview:_iv];
    [_iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_aview).offset(5.6 + 40);
        make.centerX.equalTo(_aview.mas_centerX);
        make.width.equalTo(@100);
        make.height.equalTo(@95);
    }];
    
    UILabel *ab = [UILabel new];
    ab.text = @"向商家出示二维码券即可消费";
    ab.textAlignment = 1;
    ab.textColor = ColorFrom16(0x757982);
    ab.font = PFRFONT(10);
    [_aview addSubview:ab];
    [ab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iv.mas_bottom).offset(16.4);
        make.centerX.equalTo(_aview.mas_centerX);
        make.width.greaterThanOrEqualTo(@129);
        make.height.equalTo(@14);
    }];
}

//手势的代理方法
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if([touch.view isDescendantOfView:_aview])
    {
        _alert.hidden = NO;
        return NO;
    }
    else
    {
        _alert.hidden = YES;
        return YES;
    }
}

- (void)handleTap
{
    _alert.hidden = YES;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        [self.tableView.mj_header beginRefreshing];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
